package org.cch.servlet;

import org.cch.action.Action;
import org.cch.action.UserAction;
import org.cch.bean.XMLBean;
import org.cch.util.XMLUtil;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by cch on 2015/12/18.
 */

public class ActionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String urlPath = request.getServletPath();//获得servlet请求路径后面的namespace和method

        String servletPath =urlPath.substring(0,urlPath.indexOf(".action"));
        String url ="";
        
        
        try{
        	
        	//String path =getClass().getResource("/struts.xml").getPath() ;
        	Map<String, XMLBean> map = (Map<String, XMLBean>)
        			this.getServletContext().getAttribute("xmlMap");
	
				//map = XMLUtil.getXMLResult(path);
			
				
				
			
        	XMLBean xmlBean =map.get(servletPath);
        	if(xmlBean==null){
        		response.setHeader("Content-type", "text/html;charset=UTF-8");
        		PrintWriter out = response.getWriter();
        		out.print("404：找不到请求路径！\n");
        		out.flush();
                out.close();
        		throw new Exception("严重：找不到请求路径！");
        	}
        	String classType =xmlBean.getClassType();
        	String parentType =xmlBean.getParentType();
        	Map<String,String> resultMap = xmlBean.getResult();
        	Class clazz = Class.forName(classType);
        	if(!clazz.getSuperclass().getName().equals(parentType)){
        		response.setHeader("Content-type", "text/html;charset=UTF-8");
        		PrintWriter out = response.getWriter();
        		out.print("HTTP 500！\n");
        		out.flush();
                out.close();
        		throw new Exception("严重：没有继承Action！");
        	}
        	Method method=clazz.getMethod(xmlBean.getMethod(),HttpServletRequest.class,HttpServletResponse.class);
        	String result = (String) method.invoke(clazz.newInstance(),request,response);
   
        	
        	 url= resultMap.get(result);
        	
        }catch (Exception e){
        	e.printStackTrace();
        }
        
       
        RequestDispatcher dispatcher =request.getRequestDispatcher(url);
        dispatcher.forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
    
    public static void main(String[] args) {
		System.out.println("1");
		
	}
}
